package com.zhaiweijia.eurekaconsumerribbon.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhaiweijia.dto.UserDto;
import com.zhaiweijia.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/list")
    @HystrixCommand(fallbackMethod="getUserListError")
    public ResultEntity userList( UserDto userDto){
        System.out.println(userDto.getName()+"="+userDto.getPageNum());
        HttpEntity httpEntity = new HttpEntity<>(userDto);
        ResultEntity body = restTemplate.postForEntity("http://EUREKA-PRODUCER1/user/list", httpEntity, ResultEntity.class).getBody();
        return body;
    }


    public ResultEntity getUserListError(UserDto userDto){
        return ResultEntity.error("服务器异常......");
    }
}
